<template>
  <el-dialog v-model="visible" title="克隆" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="100px" @keyup.enter="submitHandle()">
      <el-form-item label="属性源组" prop="srcGroupId">
        <el-select v-model="dataForm.srcGroupId" style="width: 100%" placeholder="请选择属性组">
          <el-option v-for="ds in state.propGroupList" :key="ds.value" :label="ds.label" :value="ds.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="属性组名" prop="dstGroupName">
        <el-input v-model="dataForm.dstGroupName" placeholder="属性组名"></el-input>
      </el-form-item>
      <el-form-item label="属性组码值" prop="dstGroupCode">
        <el-input v-model="dataForm.dstGroupCode" placeholder="属性组码值"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { ElMessage } from 'element-plus/es'
import {usePropGroupCloneApi, usePropGroupOptionsApi, usePropItemInfoApi, usePropItemSubmitApi} from '@/api/prop'
import {Option} from "@/types/common";

const emit = defineEmits(['refreshDataList'])

const visible = ref(false)
const dataFormRef = ref()

const dataForm = reactive({
  // 源组
  srcGroupId: '',
  // 目标组名
  dstGroupName: '',
  // 属性组码值
  dstGroupCode: ''
})

const state = reactive({
  propGroupList: [] as Option[]
})

/**
 * 初始化
 */
const init = () => {
  visible.value = true

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields()
  }

  // 查询全部的属性组
  usePropGroupOptionsApi().then(res => {
    state.propGroupList = res.data
  })
}

const dataRules = ref({
})

// 表单提交
const submitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false
    }
    usePropGroupCloneApi(dataForm).then(res => {
      ElMessage.success({
        message: '克隆成功',
        duration: 500,
        onClose: () => {
          visible.value = false
          emit('refreshDataList')
        }
      })
    })
  })
}

defineExpose({
  init
})
</script>
